package com.ybl.store.backend.controller.store;

import com.ybl.store.backend.dao.entity.store.ProductDO;
import com.ybl.store.backend.dto.req.store.ProductSaveReqDTO;
import com.ybl.store.backend.dto.req.store.ProductUpdateReqDTO;
import com.ybl.store.backend.dto.resp.store.ProductNameRespDTO;
import com.ybl.store.backend.service.store.ProductService;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/product")
public class ProductController {

    private final ProductService productService;

    @PostMapping
    public Result<Void> saveProduct(@RequestBody ProductSaveReqDTO requestParam) {
        productService.saveProduct(requestParam);
        return Results.success();
    }

    @DeleteMapping
    public Result<Void> deleteProduct(@RequestParam Long id) {
        productService.deleteProduct(id);
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateProduct(@RequestBody ProductUpdateReqDTO requestParam) {
        productService.updateProduct(requestParam);
        return Results.success();
    }

    @PutMapping("/ban")
    public Result<Void> banProduct(@RequestParam Long id, @RequestParam Integer banFlag) {
        productService.banProduct(id, banFlag);
        return Results.success();
    }

    @GetMapping("/page")
    public Result<ListRespDTO<ProductDO>> pageProduct(
            @RequestParam("pn") Integer pn,
            @RequestParam("ps") Integer ps,
            @RequestParam(required = false) String name)
    {
        return Results.success(productService.pageProduct(pn, ps, name));
    }

    @GetMapping("/list/name")
    public Result<ListRespDTO<ProductNameRespDTO>> listProductName () {
        return Results.success(productService.listProductName());
    }
}
